home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / stlogin4.lzh / LAST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  1.6 KB  |  65 lines

  1. /* UNIX like 'last' program. Kees Lemmens; Aug '92
  2.  
  3.    Shows the contents of the WTMP or BTMP files in the opposite order:
  4.    The user who logged in most recently is showed on the first line.
  5.    The -b option can be used to read BTMP to examine bad logins.
  6.  
  7.    Any questions or suggestions about this program can be send to:
  8.    lemmens@dv.twi.tudelft.nl
  9. */
  10.  
  11. /* define MINT if compiling for MINT, omit for normal ATARI */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "utmp.h"
  17.  
  18. void usage(void)
  19. {    fputs("\nUsage: last [-b] [-?] <-nr> \n",stderr);
  20.     exit(1);
  21. }
  22.  
  23. void main(int argc,char *argv[])
  24. {
  25.     char *log [] = { WTMP_FILE, BTMP_FILE };
  26.     char *name[] = { "WTMP", "BTMP" };
  27.     struct utmp *w;
  28.     int mod,max,cnt = -1;
  29.     time_t st_tm;
  30.  
  31.     max=mod=0;
  32.     while(--argc>0)            /* parse options */
  33.     {    if(*argv[1]=='-')
  34.         {    switch(*(++argv[1]))
  35.             {    case '?':    usage(); break;
  36.                 case 'b':    mod=1;   break; /* show last bads */
  37.                 default :    max = -atoi(argv[1]);    break;
  38.             }
  39.             ++argv;
  40.         }
  41.     }
  42.  
  43.     utmpname(log[mod]);
  44.     if((w=getutent()) == NULL)
  45.     {    printf("Can't open %s !\n",log[mod]);
  46.         exit(1);
  47.     }
  48.     st_tm=w->ut_time;        /* log time of first entry */
  49.  
  50.     do                        /* start list at end of file */
  51.     {    lseek(utmp_fd,cnt*sizeof(struct utmp),SEEK_END);
  52.         w=getutent();
  53.         printf("%-8s %-*s %2d %2d %s",w->ut_user,
  54.         (int)sizeof(w->ut_line),w->ut_line,w->ut_pid,
  55.         w->ut_type,ctime(&w->ut_time));
  56.         if(st_tm == w->ut_time)
  57.         {    printf("\n%s begins at %s",name[mod],ctime(&st_tm));
  58.             break;             /* begin of file reached */
  59.         }
  60.     }while(cnt-- > max || max == 0);
  61.  
  62.     endutent();
  63.     exit(0);
  64. }
  65.